package org.fuxia.im.gui;


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DropMode;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileFilter;

import org.fuxia.im.common.ClientConst;
import org.fuxia.im.common.ClientContext;
import org.fuxia.im.core.ECMessageType;
import org.fuxia.im.core.entries.ECPacket;
import org.fuxia.im.core.socket.ECClient;
import org.fuxia.im.entities.User;
import org.fuxia.im.utils.ImagePathUtils;
import org.tony.common.ui.CommonFrame;

public class IndividualInfo extends CommonFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField nicknameTfd;
	private JTextField nodeTfd;
	private JTextField birthTF;
	private JTextField addressTF;
	private JTextField zipCodeTF;


	/**
	 * Create the frame.
	 */
	public IndividualInfo(final User user) {
		super();
		setResizable(false);
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 559, 402);
		setTitle("我的资料");
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContent(contentPane);
		contentPane.setLayout(new GridLayout(1, 0, 0, 0));

		JPanel rightPanel = new JPanel();
		rightPanel.setBackground(Color.WHITE);
		contentPane.add(rightPanel);
		rightPanel.setLayout(null);

		JLabel nicknameLab = new JLabel("昵  称：");
		nicknameLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		nicknameLab.setBounds(155, 20, 52, 14);
		rightPanel.add(nicknameLab);

		if(user.getNickName() == null || "".equals(user.getNickName())) {
			
		}
		nicknameTfd = new JTextField(user.getNickName());
		nicknameTfd.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		nicknameTfd.setBounds(207, 17, 120, 23);
		rightPanel.add(nicknameTfd);
		nicknameTfd.setColumns(10);

		JLabel nodeLab = new JLabel("账  号：");
		nodeLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		nodeLab.setBounds(348, 20, 52, 14);
		rightPanel.add(nodeLab);

		nodeTfd = new JTextField(user.getUserCode());
		nodeTfd.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		nodeTfd.setBounds(400, 17, 120, 23);
		nodeTfd.setEditable(false) ;
		rightPanel.add(nodeTfd);
		nodeTfd.setColumns(10);

		JLabel coldcadeLab = new JLabel("个性签名");
		coldcadeLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		coldcadeLab.setBounds(155, 46, 75, 14);
		rightPanel.add(coldcadeLab);

		final JTextArea coldcaseTA = new JTextArea(user.getPersonNote()==null?"":user.getPersonNote());
		coldcaseTA.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		coldcaseTA.setLineWrap(true);
		coldcaseTA.setBounds(155, 70, 365, 42);
		coldcaseTA.setBorder(UIManager.getBorder("FormattedTextField.border"));
		coldcaseTA.setDropMode(DropMode.INSERT);
		coldcaseTA.setColumns(47);
		coldcaseTA.setRows(2);
		rightPanel.add(coldcaseTA);

		JPanel leftPanel = new JPanel();
		leftPanel.setBounds(0, 0, 137, 354);
		rightPanel.add(leftPanel);
		leftPanel.setLayout(null);

		final JLabel head = new JLabel("");
		head.setIcon(new ImageIcon(Thread.currentThread()
				.getContextClassLoader().getResource(user.getImagePath())));
		head.setBounds(16, 17, 103, 100);
		leftPanel.add(head);

		/*JButton changeHead = new JButton("更换头像");
		changeHead.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ChangeHead change = new ChangeHead() ;
				change.setVisible(true) ;
			}
		});
		changeHead.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		changeHead.setBounds(23, 135, 89, 23);
		leftPanel.add(changeHead);*/

		JLabel sexLab = new JLabel("性  别：");
		sexLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		sexLab.setBounds(155, 150, 52, 14);
		rightPanel.add(sexLab);
		
		final JComboBox<String> sexCB = new JComboBox<String>();
		sexCB.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>() ;
		model.addElement("男") ;
		model.addElement("女") ;
		model.setSelectedItem(user.getSex()==null?"男":user.getSex()) ;
		sexCB.setModel(model);
		sexCB.setBounds(207, 146, 120, 23);
		rightPanel.add(sexCB);

		JLabel ageLab = new JLabel("年  龄：");
		ageLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		ageLab.setBounds(155, 185, 53, 15);
		rightPanel.add(ageLab);
		
		final JTextField ageTF = new JTextField(user.getAge()==null?"0":user.getAge().toString()) ;
		ageTF.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		ageTF.setBounds(207, 180, 120, 23);
		rightPanel.add(ageTF);

		JLabel birthLab = new JLabel("生  日：");
		birthLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		birthLab.setBounds(155, 220, 54, 15);
		rightPanel.add(birthLab);
		
		birthTF = new JTextField(user.getBirthDay()==null?"":user.getBirthDay());
		birthTF.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		birthTF.setBounds(207, 217, 120, 23);
		rightPanel.add(birthTF);
		birthTF.setColumns(10);

		JLabel addressLab = new JLabel("地  址：");
		addressLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		addressLab.setBounds(155, 290, 54, 15);
		rightPanel.add(addressLab);
		
		addressTF = new JTextField(user.getAddress()==null?"":user.getAddress());
		addressTF.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		addressTF.setBounds(207, 285, 313, 23);
		rightPanel.add(addressTF);
		addressTF.setColumns(10);

		JLabel zipCodeLab = new JLabel("邮  编：");
		zipCodeLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		zipCodeLab.setBounds(155, 255, 54, 15);
		rightPanel.add(zipCodeLab);

		zipCodeTF = new JTextField(user.getZipcode()==null?"":user.getZipcode());
		zipCodeTF.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		zipCodeTF.setBounds(207, 250, 168, 23);
		rightPanel.add(zipCodeTF);
		zipCodeTF.setColumns(10);
		
		if(user.getUserCode().equals(((User)ClientContext.getValue(ClientConst.CURRENT_USER)).getUserCode())) {
			JButton ok = new JButton("确定");
			ok.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
										
					ECPacket packet = new ECPacket() ;
					packet.setMessageType(ECMessageType.USER_MANAGE.name()) ;
					packet.addParam("option", "modify") ;
					packet.addParam("userCode", nodeTfd.getText()) ;
					packet.addParam("nickName", nicknameTfd.getText()) ;
					packet.addParam("personNode", coldcaseTA.getText()) ;
					packet.addParam("sex", sexCB.getSelectedItem().toString()) ;
					packet.addParam("age", ageTF.getText()) ;
					packet.addParam("birthday", birthTF.getText()) ;
					packet.addParam("zipcode", zipCodeTF.getText()) ;
					packet.addParam("address", addressTF.getText()) ;
					packet.addParam("imagePath", ImagePathUtils.getImagePath(head.getIcon().toString())) ;
					ECClient client = ClientContext.getContext().getClient() ;
					try{
						client.sendMessage(packet) ;
					}catch(Exception e1) {
						e1.printStackTrace() ;
					}
					JFileChooser chooser =  new JFileChooser() ;
					chooser.setFileFilter(new FileFilter() {
						
						
						@Override
						public String getDescription() {
							return null;
						}

						@Override
						public boolean accept(File f) {
							return false;
						}
					});
					
					
				}
			});
			ok.setFont(new Font("微软雅黑", Font.PLAIN, 13));
			ok.setBounds(374, 321, 69, 23);
			rightPanel.add(ok);
		}


		JButton cancel = new JButton("取消");
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose() ;
			}
		});
		cancel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		cancel.setBounds(451, 321, 69, 23);
		rightPanel.add(cancel);

	}

	public void paint(Graphics g){
		super.paint(g) ;
		g.setColor(Color.lightGray);
		g.drawLine(154, 164, 534, 164) ;
	}
	
	public void showError(String error) {
		JOptionPane.showMessageDialog(null, error) ;
	}
	
}
